package com.lv.jie.service.impl;

import com.lv.jie.mapper.HotMapper;
import com.lv.jie.mapper.VideoMapper;
import com.lv.jie.pojo.Hot;
import com.lv.jie.pojo.Video;
import com.lv.jie.service.VideoService;
import com.lv.jie.utils.JsonResult;
import com.lv.jie.utils.UploadFileService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 24694
 */
@Service("VideoService")
public class VideoServiceImpl implements VideoService {
    final VideoMapper videoMapper;
    final HotMapper hotMapper;
    final RedisTemplate redisTemplate;
    final UploadFileService uploadFile;

    public VideoServiceImpl(VideoMapper videoMapper, HotMapper hotMapper, RedisTemplate redisTemplate, UploadFileService uploadFile) {
        this.videoMapper = videoMapper;
        this.hotMapper = hotMapper;
        this.redisTemplate = redisTemplate;
        this.uploadFile = uploadFile;
    }

    @Override
    public JsonResult deleteByPrimaryKey(Integer id, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        int i = videoMapper.deleteByPrimaryKey(id);
        if (i != 0){
            return new JsonResult("删除成功");
        }
        return new JsonResult("删除失败请联系管理员",500);
    }

    @Override
    public JsonResult insertSelective(Integer userId, Integer typeId, MultipartFile video, String token) {
        String originalFilename = video.getOriginalFilename();

        Video file1 = new Video(null,null , typeId, userId, originalFilename, null,null,null);

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss " );
        try {
            Date time = sdf.parse(sdf.format(date));
            file1.setCreateTime(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Hot hot = new Hot(null,0);
        int insert = hotMapper.insert(hot);
        if (insert != 0 ) {
            file1.setHotId(hot.getId());
        }

        try {
            String fileUrl = uploadFile.uploadFile(video,"");
            if (fileUrl != null){
                file1.setUrl(fileUrl);
                int i = videoMapper.insert(file1);
                if (i != 0){
                    return new JsonResult("文件上传成功 路径："+fileUrl);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new JsonResult("出现错误，请联系管理员",500);
    }

    @Override
    public JsonResult selectByPrimaryKey(Integer id, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        return new JsonResult(videoMapper.selectByPrimaryKey(id));
    }

    @Override
    public JsonResult listVideo(String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        return new JsonResult(videoMapper.listVideo());
    }

    private boolean getToken(String token){
        boolean flag = false;
        Object redisCode = redisTemplate.opsForValue().get(token);
        if (redisCode != null){
            flag = true;
        }
        return !flag;
    }
}
